/* M.Nielsen April 2002 mniel@cbs.dtu.dk */

/* 
Copyright (C) 2008-2015 Danish Technical University

This suite of programs and library routine is free software. You can 
redistribute it and/or modify it under the terms of the GNU General Public 
License as published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.

In other words, you are free to modify, copy, or redistribute this
source code and its documentation in any way you like, but you must
distribute all derivative versions as free software under the same
terms that I've provided my code to you (i.e. the GNU General Public
License). This precludes any use of the code in proprietary or
commercial software unless your source code is made freely available.

If you wish to use the code under a different Open Source license
that's not compatible with the GPL (like the Artistic License, BSD
license, or the Netscape Public License), please contact me
(Morten Nielsen, mniel@cbs.dtu.dk) for permission.

Incorporation into commercial software under non-GPL terms is possible
by obtaining a specially licensed version from The Danish Technical University.
Contact Morten Nielsen (mniel@cbs.dtu.dk) to arrange licensing terms.

This software is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
*/


#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "utils.h"

FILENAME	p_mat;

PARAM   param[] = {
	"-mat", VFNAME p_mat, "File with PSSM matrix", "my.mat",
	0
};

float   **pssm_matrix_read( FILENAME filename, char **alphabet, int *len )

{
        char		**wvec;
        int             i, l, start, nc, nc1, n;
        int             alphabet_read;
	float		**mat;
	LINELIST	*linelist, *ln;
	char		c;
	float		*vec;
	LINE		restofline;

	linelist = linelist_read( filename );

	if ( ! linelist ) {
		printf( "Error. Cannot read from file %s. Exit\n", filename );
		exit( 1 );
	}

        alphabet_read = 0;
	start = 0;
	l = 0;

	for ( ln=linelist; ln; ln=ln->next ) {
                     
                if ( strlen( ln->line ) <= 1 ) continue;
                     
                if ( strncmp( ln->line, "#", 1 ) == 0 ) continue;
                     
                if ( strncmp( ln->line, "Last", 4 ) == 0 ) {
			start = 1;
			continue;
		}

		if ( start && ! alphabet_read ) {

			wvec = split( ln->line, &nc );

			(*alphabet) = cvector( 0, nc );

			for ( i=0;i<nc;i++ )
                                (*alphabet)[i] = wvec[i][0];

			(*alphabet)[nc] = 0;

                        alphabet_read = 1;

			continue;
		}

		if ( start && alphabet_read ) {

			l++;
			continue;

		}
	}

	(*len) = l;

	mat = fmatrix( 0, l-1, 0, nc-1 );

	l = 0;
	start = 0;
	alphabet_read = 0;

	for ( ln=linelist; ln; ln=ln->next ) {
                     
                if ( strlen( ln->line ) <= 1 ) continue;
                                
                if ( strncmp( ln->line, "#", 1 ) == 0 ) continue;
                                
                if ( strncmp( ln->line, "Last", 4 ) == 0 ) {
                        start = 1;
                        continue;
                }               

                if ( start && ! alphabet_read ) {
			alphabet_read = 1;
			continue;
		}

		if ( start && alphabet_read ) {

			sscanf( ln->line, "%i %c %[^\n]", &n, &c, restofline );

			vec = fvector_split( restofline, &nc1 );

			if ( nc1 != nc ) {
				printf( "Inconsistent line format %i %i %s\n", nc, nc1, ln->line );
				exit( 1 );
			}

			for ( i=0; i<nc; i++ )
				mat[l][i] = vec[i];

			fvector_free( vec, 0, nc-1 );

			l++;
		}
	}

	printf( "# PSSM read from file %s. Alphabet %s. Len %i\n", filename, *alphabet, *len );

	return( mat );
}

main( int argc, char *argv[] )

{

	float	**mat;
	char	*alphabet;
	int	plen;
	PEPLIST	*peplist, *pl;
	float	s;
	int	i, nlines;
	int	c;

	pparse( &argc, &argv, param, 1, "input_file" );

	/* Read peptide list from file */
	/* Peplist list element has the structure
		pl->pep peptide amino acid sequence
		pl->score peptide binding affinity 
		pl->len peptide length
	*/

	/* Read peplist from file */
	peplist = peplist_read( argv[1] );

	/* Read PSSM matrix in the format made by the pep2mat program */
	/* The amino acid order is returned in the variable alphabet (a string) */

	mat = pssm_matrix_read( p_mat, &alphabet, &plen );

	nlines = 0;

	for ( pl=peplist; pl; pl=pl->next ) {

		if ( pl->len != plen ) {
			printf( "Inconsistent length Pep %i Mat %i. Exit\n", pl->len, plen );
			exit( 1 );
		}

		s = 0;

		/* Score peptide to PSMM */
		for ( i=0; i<plen; i++ ) {
			
			/* HERE YOU MUST WRITE THE MSSING CODE IDENTIFYING THE POSITION OF THE 
			AMINO ACID I IN THE BLOUSM ALPHABET
			*/
			c = strpos(alphabet , pl->pep[i]);

			if ( c < 0 ) {
				printf( "Error. Sequence letter unknown %c %s\n",
                                               pl->pep[i], pl->pep );
				exit( 1 );
			}
			else {
				s += mat[i][c];
			}

		}

		printf( "%s %f", pl->pep, s );

		/* If peptide input has a binding affinity values associated, print this value also? */
		if ( pl->score != -99.9 )
			printf( " %f", pl->score );

		printf( "\n" );

		nlines++;

	}

	printf( "# Number of lines %i\n", nlines );

	exit( 0 );
}
